﻿using EyFramework.Common;
using EyFramework.Library;
using System;
using System.Windows.Forms;

namespace EyFramework.Main
{
    public partial class FrmLock : FrmDevBase
    {
        public FrmLock()
        {
            InitializeComponent();
        }

        private void btnUnlock_Click(object sender, EventArgs e)
        {
            CheckPassword();
        }

        private void CheckPassword()
        {
            if (string.IsNullOrEmpty(tePassword.Text.Trim()))
            {
                Globals.ShowWarning("请输入解锁密码！");
                tePassword.Focus();
                return;
            }

            if (tePassword.Text.Trim() == Globals.CurrentUser.Password)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                Globals.ShowError("登录密码校验失败，请确认密码是否正确！");
                tePassword.Focus();
            }
        }

        private void FrmLockDev_Load(object sender, EventArgs e)
        {
            teAccount.Text = Globals.CurrentUser.LoginName;
        }

        private void FrmLockDev_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult != DialogResult.OK)
            {
                if (Globals.ShowYesNoAndWarning(string.Format("{0},正在使用本应用，退出用户未保存的数据将全部丢失！是否继续？", Globals.CurrentUser.StaffName)) != DialogResult.Yes)
                {
                    e.Cancel = true;
                }
            }
        }

        private void tePassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                CheckPassword();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}